#include "gtest/gtest.h"
#include "../BaseUtil/MyTest.h"
#include "DOMCoreImp.h"
namespace DOMCoreImp {

class CharacterDataImpTest : public testing::Test {
protected:
	class DummyCharacterDataImp : public CharacterDataImp {
	public:
		DummyCharacterDataImp(DOMString nodeName, DOMString data, DocumentImp * ownerDocument)
		: CharacterDataImp( nodeName, data, ownerDocument ) {}

		virtual unsigned short getNodeType() { return 99; }
	};

    virtual void SetUp() {
    	charData = new DummyCharacterDataImp( "DUMMY_CHAR_ELEMENT", "CharacterDataName", NULL );
    }
    virtual void TearDown() {
    	delete charData;
    }

    CharacterDataImp * charData;
};

TEST_F(CharacterDataImpTest, Inheritance ) {
	CharacterDataImpPtr( new DummyCharacterDataImp( "", "", NULL ) );
	CharacterDataPtr( new DummyCharacterDataImp(  "", "", NULL ) );
	NodeImpPtr( new DummyCharacterDataImp(  "", "", NULL ) );
	NodePtr( new DummyCharacterDataImp(  "", "", NULL ) );
}

TEST_F(CharacterDataImpTest, GetSetData) {
	charData->setData( "Anything" );
	ASSERT_STREQ( "Anything", charData->getData().c_str() );
}

TEST_F(CharacterDataImpTest, Length) {
	charData->setData( "9876543210" );
	ASSERT_EQ( 10ul, charData->getLength() );
}

TEST_F(CharacterDataImpTest, SubstringData) {
	charData->setData( "123456789" );
	ASSERT_STREQ( "3456", charData->substringData(2, 4).c_str() );

	ASSERT_THROW( charData->substringData( 9, 1 ), DOMExceptionPtr );
	charData->substringData( 8, 1 );
}

TEST_F(CharacterDataImpTest, AppendData) {
	charData->setData( "123" );
	charData->appendData( "45" );
	ASSERT_STREQ( "12345", charData->getData().c_str() );
}

TEST_F(CharacterDataImpTest, InsertData) {
	charData->setData( "123" );
	charData->insertData( 2, "45" );
	ASSERT_STREQ( "12453", charData->getData().c_str() );

	ASSERT_THROW( charData->insertData( 6, "1" ), DOMExceptionPtr );
	charData->insertData( 5, "1" );
}

TEST_F(CharacterDataImpTest, DeleteData) {
	charData->setData( "1234" );
	charData->deleteData( 1, 2 );
	ASSERT_STREQ( "14", charData->getData().c_str() );

	ASSERT_THROW( charData->deleteData( 2, 1 ), DOMExceptionPtr );
	charData->deleteData( 1, 1 );
}

TEST_F(CharacterDataImpTest, ReplaceData) {
	charData->setData( "1234" );
	charData->replaceData( 1, 2, "ABCD" );
	ASSERT_STREQ( "1ABCD4", charData->getData().c_str() );

	ASSERT_THROW( charData->replaceData( 6, 1, "A" ), DOMExceptionPtr );
	charData->replaceData( 5, 1, "A" );
}


}
